#ifndef PSPGRAPHICS_H
#define PSPGRAPHICS_H

#include "Graphics.h"
#include "Application.h"

#include <vector>
#include <algorithm>

#define MAX_QUADS 4096

namespace GenBen {

class PSPTexture : public Texture
{
public:
	PSPTexture(const string& filename);
	virtual ~PSPTexture();

	virtual uint getWidth() { return width; };
	virtual uint getHeight() { return height; };

	virtual void release() { if(data) free(data); data = NULL; };

	void* getData() { return data; };

private:
	uint width, height;
	void* data;
};

struct PSPRect
{
	RectI source, dest;
	uint tint;
	PSPTexture* tex;
	int layer;

	bool operator<(const PSPRect& rect) const
	{
		if(layer != rect.layer)
			return layer < rect.layer;

		return tex < rect.tex;
	}
};

class PSPGraphics : public Graphics
{
public:
	PSPGraphics();
	virtual ~PSPGraphics();

	virtual Texture* loadTexture(string filename);

	virtual string getDescription();
	
	virtual void clear(const Color& color);
	virtual void present();

	virtual void draw(Texture* texture, int layer, const RectI& source, const VectorI& dest, const Color& tint);
	virtual void draw(Texture* texture, int layer, const RectI& source, const RectI& dest, const Color& tint);
	virtual void draw(Texture* texture, int layer, const RectI& source, const VectorI& dest);
	virtual void draw(Texture* texture, int layer, const RectI& source, const RectI& dest);

private:
	uint rect_count;
	std::vector<PSPRect> rects;
	void* fbp0;
	void* fbp1;
	void* zbp;

	bool clear_flag;
};

}

#endif
